package com.lyp.supermanage.view;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.supermanage.R;
import com.lyp.supermanage.api.Api;
import com.lyp.supermanage.bean.DetailsBean;
import com.lyp.supermanage.bean.ReportDetailsBean;
import com.lyp.supermanage.utils.uitl;

import java.text.DecimalFormat;
import java.util.ArrayList;

import cn.qqtheme.framework.picker.OptionPicker;
import cn.qqtheme.framework.widget.WheelView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ReportDetailsActivity extends Activity implements View.OnClickListener{

    private Button mYear,mMonth,mDay,mSelect;
    private ArrayList<ReportDetailsBean.DataBean>  mList = new ArrayList<>();
    private DetailsBean.DataBean mBean;
    private TextView mReports,mReportsMoney,mSucceedMoney,mSucceed,mCharge,mChargMoney;
    private ImageView mBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cardetails);
        initview();
    }

    private void initview() {
        mBean = (DetailsBean.DataBean)getIntent().getExtras().get("databean");
        mYear = (Button) findViewById(R.id.btn_my_year);
        mMonth = (Button) findViewById(R.id.btn_my_month);
        mDay = (Button) findViewById(R.id.btn_my_day);
        mSelect = (Button) findViewById(R.id.btn_my_select);
        mReports = (TextView) findViewById(R.id.tv_reports_number);
        mReportsMoney = (TextView) findViewById(R.id.tv_reports_money);
        mSucceedMoney = (TextView) findViewById(R.id.tv_succeed_number);
        mSucceed = (TextView) findViewById(R.id.tv_succeed_money);
        mCharge = (TextView) findViewById(R.id.tv_chargeback_number);
        mChargMoney = (TextView) findViewById(R.id.tv_chargeback_money);
        mBack = (ImageView) findViewById(R.id.iv_detailsback);
        mYear.setOnClickListener(this);
        mMonth.setOnClickListener(this);
        mDay.setOnClickListener(this);
        mSelect.setOnClickListener(this);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        myreport(mBean.getId(),1, "2017");
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
            case R.id.btn_my_year:
                onYearPicker(arg0);
                break;
            case R.id.btn_my_month:
                onMonthPicker(arg0);
                break;
            case R.id.btn_my_day:
                onDayPicker(arg0);
                break;
            case R.id.btn_my_select:
                String year = mYear.getText().toString().trim();
                String month = mMonth.getText().toString();
                String day = mDay.getText().toString();
                String date = year;
                int type = 1;
                if (month.equals("整")&&!day.equals("整")){
                    type = 1;
                    date = year;
                }
                else if (!month.equals("整")&&day.equals("整")){
                    type = 2;
                    date += "-"+month;
                }
                else if (!month.equals("整")&&!day.equals("整")){
                    type = 3;
                    date += "-"+month+"-"+day;
                }
                myreport(mBean.getId(), type, date);
                break;
        }
    }

    public void onYearPicker(View view) {
        final String[] provices = new String[]{"2017", "2018", "2019", "2020", "2021","2022","2023","2024","2025"};
        OptionPicker picker = new OptionPicker(this, provices);
        picker.setCanceledOnTouchOutside(false);
        picker.setDividerRatio(WheelView.DividerConfig.FILL);
        picker.setShadowColor(Color.WHITE, 40);
        picker.setSelectedIndex(0);
        picker.setCycleDisable(true);
        picker.setTitleText("年选择");
        picker.setTitleTextSize(ConvertUtils.sp2px(10));
        picker.setTextSize(ConvertUtils.sp2px(10));
        picker.setPadding(ConvertUtils.sp2px(10));
        picker.setCancelTextSize(ConvertUtils.sp2px(10));
        picker.setSubmitTextSize(ConvertUtils.sp2px(10));
        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                mYear.setText(provices[index]);
            }
        });
        picker.show();
    }

    public void onMonthPicker(View view) {
        final String[] provices = new String[]{"整","01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        OptionPicker picker = new OptionPicker(this, provices);
        picker.setCanceledOnTouchOutside(false);
        picker.setDividerRatio(WheelView.DividerConfig.FILL);
        picker.setShadowColor(Color.WHITE, 40);
        picker.setSelectedIndex(0);
        picker.setCycleDisable(true);
        picker.setTitleText("月选择");
        picker.setTitleTextSize(ConvertUtils.sp2px(10));
        picker.setTextSize(ConvertUtils.sp2px(10));
        picker.setPadding(ConvertUtils.sp2px(10));
        picker.setCancelTextSize(ConvertUtils.sp2px(10));
        picker.setSubmitTextSize(ConvertUtils.sp2px(10));
        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                mMonth.setText(provices[index]);
            }
        });
        picker.show();
    }

    public void onDayPicker(View view) {
        if (mMonth.getText().toString().equals("整")) {
            ToastUtils.showShortSafe("请先选择月份!");
            return;
        }
        uitl uitl = new uitl();
        int ri = Integer.parseInt(uitl.calDays(mYear.getText().toString().trim(),mMonth.getText().toString().trim()));
        ArrayList<String> mList = new ArrayList<>();
        final String[] str = new String[ri + 1];
        mList.add("整");
        str[0] = "整";
        for (int i = 1; i < ri; i++) {
            mList.add((i) + "");
            str[i] = (i) + "";
        }
        OptionPicker picker = new OptionPicker(this, mList);
        picker.setCanceledOnTouchOutside(false);
        picker.setDividerRatio(WheelView.DividerConfig.FILL);
        picker.setShadowColor(Color.WHITE, 40);
        picker.setSelectedIndex(0);
        picker.setCycleDisable(true);
        picker.setTitleText("日选择");
        picker.setTitleTextSize(ConvertUtils.sp2px(10));
        picker.setTextSize(ConvertUtils.sp2px(10));
        picker.setPadding(ConvertUtils.sp2px(10));
        picker.setCancelTextSize(ConvertUtils.sp2px(10));
        picker.setSubmitTextSize(ConvertUtils.sp2px(10));
        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                if (index != 0) {
                    mDay.setText(new DecimalFormat("00").format(Integer.parseInt(str[index])));
                } else {
                    mDay.setText(str[index]);
                }
            }
        });
        picker.show();
    }

    //销售员报告
    public void myreport(String a_id,int type,String datetime) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Api api = retrofit.create(Api.class);
        Call<ReportDetailsBean> call = api.getReportDetails(a_id,type,datetime);
        call.enqueue(new Callback<ReportDetailsBean>() {
            @Override
            public void onResponse(Call<ReportDetailsBean> call, final Response<ReportDetailsBean> response) {
                if (response.body().getResult() == 1 ) {
                    ReportDetailsActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mList.clear();
                            mList.add(response.body().getData());
                            mReports.setText(mList.get(0).getOrderNum());
                            mReportsMoney.setText(mList.get(0).getOrderPrice());
                            mSucceedMoney.setText(mList.get(0).getFinishNum());
                            mSucceed.setText(mList.get(0).getFinishPrice());
                            mCharge.setText(mList.get(0).getQuitNum());
                            mChargMoney.setText(mList.get(0).getQuitPrice());
                        }
                    });
                } else{
                    ToastUtils.showShort(response.body().getMessage());
                }
            }
            @Override
            public void onFailure(Call<ReportDetailsBean> call, Throwable t) {
                ToastUtils.showShort(t.getMessage());
            }
        });
    }
}
